package controller;

import dao.DrugDAOImpl;
import model.TDrug;
import utils.DispatcherUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "SaveDrugServlet", value = "/savedrug.do")
public class SaveDrugServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //String utid = request.getParameter("utid");
        String drugid = request.getParameter("drugid");//药品ID
        String userid = request.getParameter("userid");//用户ID
        String  name = request.getParameter("name");//药品名称
        String  img = request.getParameter("img");//药品图片
        String  drugclass = request.getParameter("drugclass");//药品类别
        String cureclass = request.getParameter("cureclass");//治疗类别
        String drugsize = request.getParameter("drugsize");//药品规格
        String fexplain = request.getParameter("fexplain");//功效说明

        String merchant = request.getParameter("merchant");//厂家
        String price = request.getParameter("price");//价格

        if(drugid.equals("0")){
            TDrug tDrug = new TDrug();
            DrugDAOImpl drugDAO = new DrugDAOImpl();

            tDrug.setUserid(userid);//用户ID
            tDrug.setName(name);//药品名称
            tDrug.setImg(img);//药品图片
            tDrug.setDrugclass(drugclass);//药品类别
            tDrug.setCureclass(cureclass);//治疗类别
            tDrug.setDrugsize(drugsize);//药品规格
            tDrug.setFexplain(fexplain);//功效说明
            tDrug.setMerchant(merchant);//厂家
            tDrug.setPrice(Integer.parseInt(price));

            if(name.equals("")||name==null||img.equals("")||img==null
                    ||price.equals("")||price==null||drugclass.equals("")||drugclass==null||cureclass.equals("")||
                    cureclass==null || drugsize.equals("") ||drugsize == null || fexplain.equals("") ||fexplain == null
                    || merchant.equals("") ||merchant == null
            ){
                DispatcherUtils.openErrWeb("药品名称，药品图片，药品类别，治疗类别，药品规格,厂家,功效说明,价格不能为空","addgoods.jsp",request,response);
            }

            if(drugDAO.addDrug(tDrug)>=0){
                response.sendRedirect("index.do");
            }else{
                DispatcherUtils.openErrWeb("商品添加失败，请检查商品数据","addgoods.jsp",request,response);
            }

        }else{

            TDrug tDrug = new TDrug();
            DrugDAOImpl drugDAO = new DrugDAOImpl();

            tDrug.setDrugid(Integer.parseInt(drugid));
            tDrug.setUserid(userid);//用户ID
            tDrug.setName(name);//药品名称
            tDrug.setImg(img);//药品图片
            tDrug.setDrugclass(drugclass);//药品类别
            tDrug.setCureclass(cureclass);//治疗类别
            tDrug.setDrugsize(drugsize);//药品规格
            tDrug.setFexplain(fexplain);//功效说明
            tDrug.setMerchant(merchant);//厂家
            tDrug.setPrice(Integer.parseInt(price));

            if(name.equals("")||name==null||img.equals("")||img==null
                    ||price.equals("")||price==null||drugclass.equals("")||drugclass==null||cureclass.equals("")||
                    cureclass==null || drugsize.equals("") ||drugsize == null || fexplain.equals("") ||fexplain == null
                    || merchant.equals("") ||merchant == null
            ){
                DispatcherUtils.openErrWeb("药品名称，药品图片，药品类别，治疗类别，药品规格,厂家,功效说明,价格不能为空","addgoods.jsp",request,response);
            }

            if(drugDAO.updateDrug(tDrug)){
                response.sendRedirect("index.do");
            }else{
                DispatcherUtils.openErrWeb("商品修改失败，请检查商品数据","addgoods.jsp",request,response);
            }

        }


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
